package main

import (
	"fmt"
	"go-web-learning/src/04_gorm/db"
)

var DB = db.DB

func main() {
	JoinTableCrud()
}

func createTable() {
	// 建表
	//DB.AutoMigrate(&db.User{})
	//DB.AutoMigrate(&db.Article{})
}

func One2Many() {
	// 一对多添加
	// 创建用户并创建文章
	//DB.Create(&db.User{
	//	Name: "talon",
	//	Articles: []db.Article{
	//		{Title: "title1", Content: "content1"},
	//		{Title: "title2", Content: "content2"},
	//	},
	//})

	// 创建文章，并关联以前的用户
	//DB.Create(&db.Article{
	//	Title:   "title3",
	//	Content: "content3",
	//	UserID:  2,
	//})
	//var user db.User
	//DB.Take(&user, 2)
	//DB.Create(&db.Article{
	//	Title:   "title4",
	//	Content: "content4",
	//	User:    user,
	//})

	// 给现有用户绑定文章
	//var user db.User
	//var article db.Article
	//DB.Take(&user, 3)
	//DB.Take(&article, 1)

	// 方法1
	//user.Articles = []db.Article{article}
	//DB.Save(&user)

	// 方法2
	//DB.Model(&user).Association("Articles").Append(&article)

	// 给文章绑定用户
	//DB.Model(&article).Association("User").Append(&user)

	// 查询
	//var user db.User
	//DB.Preload("Articles", "id = ?", 2).Take(&user)
	//fmt.Println(user)
	//
	//var user db.User
	//DB.Preload("Articles", func(db *gorm.DB) *gorm.DB {
	//	return db.Where("title in ?", []string{"title2", "title4"})
	//}).Take(&user)
	//fmt.Println(user)

	// 清除外键关系
	//var user db.User
	//DB.Preload("Articles").Take(&user, 3)
	//DB.Model(&user).Association("Articles").Delete(&user.Articles)
	//DB.Delete(&user)

	// 级联删除
	var user db.User
	DB.Take(&user, 2)
	DB.Select("Articles").Delete(&user)
}

func One2One() {
	//建表
	//DB.AutoMigrate(&db.User{}, &db.UserInfo{})
	//DB.AutoMigrate(&gorm.Model{})

	// 添加数据
	DB.Create(&db.User{
		Name:     "talon",
		Articles: nil,
		UserInfo: db.UserInfo{
			Like: "ctrl",
			Addr: "上海市",
		},
	})

	//var user db.User
	//DB.Preload("UserInfo").Take(&user)
	//fmt.Println(user)

	//var user db.User
	//DB.Take(&user)
	//DB.Select("UserInfo").Delete(&user)

}

func Many2Many() {
	//DB.AutoMigrate(&db.Article{}, &db.Tag{})

	// 创建文章同时创建标签并关联
	//DB.Create(&db.Article{
	//	Title:   "python学习",
	//	Content: "python学习",
	//	UserID:  4,
	//	Tags: []db.Tag{
	//		{Name: "学习资料"},
	//		{Name: "python"},
	//	},
	//})

	// 创建文章并关联已有标签
	//var tags []db.Tag
	//DB.Where("id in ?", []int{1, 6}).Find(&tags)
	//tags = append(tags, db.Tag{Name: "自律的一天"})
	//DB.Create(&db.Article{
	//	Title:   "go学习",
	//	Content: "go学习",
	//	UserID:  4,
	//	Tags:    tags,
	//})

	//var article db.Article
	//DB.Preload("Tags").Take(&article)
	//fmt.Println(article)

	// 多对多删除
	// 一般情况就是先删除原有的关联关系，再插入新的关系
	//// 先查询
	//var article db.Article
	//DB.Preload("Tags").Take(&article)
	////再删除
	//DB.Model(&article).Association("Tags").Delete(article.Tags)
	//// 添加新的
	//var tags []db.Tag
	//DB.Where("name like ?", "%自学%").Find(&tags)
	//fmt.Println(tags)
	//DB.Model(&article).Association("Tags").Append(&tags)

	// 使用Replace
	// 查询文章
	var article db.Article
	DB.Take(&article)
	// 查询出标签
	var tags []db.Tag
	DB.Where("name not like ?", "%自学%").Find(&tags)
	fmt.Println(tags)
	// 替换
	DB.Model(&article).Association("Tags").Replace(&tags)
}

func Many2ManyCustom() {
	DB.SetupJoinTable(&db.Person{}, "Addresses", &db.PersonAddress{})
	DB.AutoMigrate(&db.Person{}, &db.Address{}, &db.PersonAddress{})

	//DB.Create(&db.Person{
	//	Name: "小王",
	//	Addresses: []db.Address{
	//		{Name: "上海"},
	//		{Name: "山东"},
	//		{Name: "北京"},
	//	},
	//})

	//var addresses []db.Address
	//DB.Find(&addresses, 1)
	//DB.Create(&db.Person{Name: "小李", Addresses: addresses})

	// 查询person
	//var person db.Person
	//DB.Take(&person, 2)
	//// 查询地址
	//var addresses []db.Address
	//DB.Find(&addresses, 2)
	//// 绑定
	//DB.Model(&person).Association("Addresses").Append(&addresses)

	// 查询person
	var person db.Person
	DB.Take(&person, 2)
	// 查询地址
	var addresses []db.Address
	DB.Find(&addresses, 3)
	// 绑定
	DB.Model(&person).Association("Addresses").Replace(&addresses)
	//
	//var people []db.Person
	//DB.Preload("Addresses").Find(&people)
	//fmt.Println(people)
}

func Many2ManyATCustom() {
	DB.SetupJoinTable(&db.ArticleModel{}, "Tags", &db.ArticleTagModel{})
	DB.AutoMigrate(&db.ArticleModel{}, &db.TagModel{}, &db.ArticleTagModel{})

	//DB.Create(&db.ArticleModel{
	//	Title: "python学习",
	//	Tags: []db.TagModel{
	//		{Name: "python"},
	//		{Name: "自学"},
	//	},
	//})

	//var tags []db.TagModel
	//DB.Find(&tags, "name = ?", "自学")
	//DB.Create(&db.ArticleModel{Title: "golang学习", Tags: tags})

	////查询
	//var article db.ArticleModel
	//DB.Take(&article, 2)
	//// 查询
	//var tags []db.TagModel
	//DB.Find(&tags, "name like ?", "%go%")
	//// 绑定
	//DB.Model(&article).Association("Tags").Append(&tags)

	// 查询
	var article db.ArticleModel
	DB.Take(&article, 2)
	// 查询
	var tags []db.TagModel
	DB.Find(&tags, "name like ?", "%go%")
	// 绑定
	DB.Model(&article).Association("Tags").Replace(&tags)

	//var people []db.Person
	//DB.Preload("Addresses").Find(&people)
	//fmt.Println(people)
}

func JoinTableCrud() {
	DB.SetupJoinTable(&db.UserModel{}, "Collects", &db.UserCollectModel{})
	//DB.AutoMigrate(&db.UserModel{}, &db.ArticleModel{}, &db.UserCollectModel{})

	var userCollects []db.UserCollectModel
	DB.Preload("Article").Preload("User").Find(&userCollects, 1)
	fmt.Println(userCollects)
}
